package appapi.controller.oider;

import appapi.Global.BusinessException;
import appapi.Request.InspectionPageReq;
import appapi.Request.SelfcheckPageReq;
import appapi.Vo.AddSelfcheckRecVo;
import appapi.Vo.EditSelfcheckRecVo;
import appapi.Vo.basic.ExcelSelfcheckVo;
import appapi.Vo.record.InspectWarnVo;
import appapi.Vo.record.MonthInspWarnVo;
import appapi.common.BusinessTypeEnum;
import appapi.common.ResponseBean;
import appapi.config.CurrentUser;
import appapi.config.MyLog;
import appapi.dto.DelSelfCheckDto;
import appapi.entity.Zhongjiao.SelfcheckDto;
import appapi.entity.app.RectificationEntity;
import appapi.entity.basic.SelfcheckItemEntity;
import appapi.entity.basic.UserInfo;
import appapi.model.ResponseDataModel;
import appapi.model.ResponseModel;
import appapi.service.ISelfcheckService;
import appapi.service.IStaffService;
import appapi.service.impl.StaffService;
import appapi.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 设备自检记录相关功能
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/06/02 12:56
 */
@Api(value = "/api/selfCheck", tags = "设备自检记录")
@RestController
@RequestMapping(value = "/api/selfCheck")
public class SelfCheckController {

    @Resource
    private ISelfcheckService iSelfcheckService;
    @Resource
    IStaffService istaffService;

    //region 设备自检

    /**
     * 分页获取设备自检记录
     *
     * @return
     * @auther djq 2023/5/17
     */
    @ApiOperation(value = "自检-分页查询", notes = "自检-分页查询")
    @GetMapping(value = "/GetSelfcheckListByPage")
    public ResponseBean GetSelfcheckListByPage(@CurrentUser UserInfo userInfo, SelfcheckPageReq selfcheckPageReq) {
        try {
            if (selfcheckPageReq.getCurrPage() == null || selfcheckPageReq.getPageSize() == null) {
                selfcheckPageReq.setCurrPage(1);
                selfcheckPageReq.setPageSize(10);
            }
            //如果什么都没传,则查到该账号的所属机构显示数据
            if (selfcheckPageReq.getOrgaPathIds() == null) {
                //拿到人员信息
                Map<String, Object> staffDto = istaffService.SelectStaffDetail(userInfo.getUserid());
                String orgaPathIds = staffDto.get("orgaPathIds").toString();
                selfcheckPageReq.setOrgaPathIds(orgaPathIds);
            }
            List<Map<String, Object>> projects = iSelfcheckService.SelectSelfcheckPage(selfcheckPageReq);

            PageInfo pageInfo = new PageInfo(projects, selfcheckPageReq.getPageSize());

            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("分页获取设备自检记录异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 设备自检导出
     *
     * @return
     * @author ygp 2023/06/12
     */
    @ApiOperation(value = "设备自检导出", notes = "设备自检导出接口", produces = "application/octet-stream")
    @GetMapping(value = "/SelfRecExport")
    @ApiOperationSupport(ignoreParameters = {"currPage", "pageSize"})
    public ResponseBean SelfRecExport(SelfcheckPageReq req, HttpServletResponse response) {
        try {
            List<ExcelSelfcheckVo> list = iSelfcheckService.getList(req);
            if (list.size() == 0) {
                return ResponseBean.success("数据源为空");
            } else if (list.size() > 5000) {
                return ResponseBean.fail("记录超过5000，请重新选择查询条件");
            } else {
                ExcelUtils.exportExcel(list, "设备自检", "设备自检sheet", ExcelSelfcheckVo.class, "设备自检", response);
                return ResponseBean.success();
            }
        } catch (Exception e) {
            return ResponseBean.fail("设备自检导出异常");
        }
    }

    /**
     * 获取操作员自检记录列表
     *
     * @return
     * @auther djq 2023/5/17
     */
    @ApiOperation(value = "自检-列表查询", notes = "自检-列表查询")
    @GetMapping(value = "/GetSelfcheckList")
    public ResponseModel GetSelfcheckList(Long equipId) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            List<Map<String, Object>> list = iSelfcheckService.SelectSelfcheckList(equipId);
            responseModel.setData(list);
            responseModel.setRcount(list.size());
        } catch (BusinessException exception) {
            responseModel.setMessage("获取操作员自检记录列表异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }

    /**
     * 添加自检记录
     *
     * @return
     * @auther djq 2023/5/17
     */
    @ApiOperation(value = "自检-添加记录", notes = "自检-添加记录")
    @PostMapping(value = "/AddSelfcheck")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "vo", dataType = "object", value = "自检参数")
    })
    @MyLog(title = "自检添加",businessType = BusinessTypeEnum.insert)
    public ResponseBean AddSelfcheck(@CurrentUser UserInfo userInfo,@Validated @RequestBody AddSelfcheckRecVo vo) {
        try {
            vo.setCreateUser(userInfo.getUsername());
            int iRet = iSelfcheckService.InsertSelfcheck(vo);
            if (iRet > 0) {
                return ResponseBean.addSuccess();
            } else {
                return ResponseBean.fail("保存失败");
            }
        } catch (BusinessException exception) {
            return ResponseBean.fail(exception.getMessage());
        }
    }

    @ApiOperation(value = "自检-获取记录详情", notes = "自检-获取记录详情")
    @GetMapping(value = "/GetSelfcheckDetail")
    public ResponseDataModel<SelfcheckDto> GetSelfcheckDetail(Long selfId) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            SelfcheckDto dto = iSelfcheckService.getSelfCheckModel(selfId);
            responseModel.setData(dto);
        } catch (BusinessException exception) {
            responseModel.setMessage("获取记录详情异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }

    /**
     * 编辑自检记录
     *
     * @return
     * @auther djq 2023/5/17
     */
    @ApiOperation(value = "自检-编辑自检记录", notes = "自检-编辑自检记录")
    @PostMapping(value = "/EditSelfcheck")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "dto", dataType = "object", value = "自检参数")
    })
    @MyLog(title = "自检编辑",businessType = BusinessTypeEnum.update)
    public ResponseBean EditSelfcheck(@CurrentUser UserInfo userInfo,@Validated @RequestBody EditSelfcheckRecVo dto) {
        try {
            dto.setCreateUser(userInfo.getUsername());
            int iRet = iSelfcheckService.UpdateSelfcheck(dto);
            if (iRet > 0) {
                return ResponseBean.updateSuccess();
            } else {
                return ResponseBean.fail("更新失败");
            }
        } catch (BusinessException exception) {
            return ResponseBean.fail(exception.getMessage());
        }
    }

    /**
     * 删除自检记录
     *
     * @return
     * @auther djq 2023/5/17
     */
    @ApiOperation(value = "自检-删除自检记录", notes = "自检-删除自检记录")
    @PostMapping(value = "/DeleteSelfcheck")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "selfId", dataType = "Long", value = "自检记录id", required = true)
    })
    @MyLog(title = "删除自检记录",businessType = BusinessTypeEnum.delete)
    public ResponseBean DeleteSelfcheck(DelSelfCheckDto dto) {
        try {
            int iRet = iSelfcheckService.DelSelfcheck(dto);
            if (iRet > 0) {
                return ResponseBean.deleteSuccess();
            } else {
                return ResponseBean.fail("删除失败");
            }
        } catch (BusinessException exception) {
            return ResponseBean.fail(exception.getMessage());
        }
    }

    /**
     * 获取自检统计
     *
     * @return
     * @author djq 2023/06/06
     */
    @ApiOperation(value = "自检--获取自检统计", notes = "自检--获取自检统计")
    @GetMapping(value = "/GetSeflCheckWarn")
    public ResponseBean GetWarn(@CurrentUser UserInfo userInfo) {
        try {
            InspectWarnVo resData = iSelfcheckService.GetWarn(userInfo);
            return ResponseBean.success(resData);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取自检统计异常: ".concat(exception.getMessage()));
        }
    }

    //endregion
}
